home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / usertermination.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  5.2 KB  |  174 lines

  1. /*
  2.     File:        UserTermination.cp
  3.  
  4.     Contains:    TUserTermination is a command dot tracking class, used for finding out if the end user wants to stop
  5.                   an action. Most of the code is based on earlier snippet by Dave Radcliffe/DTS.
  6.                   TUserTermination.cp contains class body information for the TUserTermination class.
  7.  
  8.     Written by: Kent Sandvik    
  9.  
  10.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #ifndef _USERTERMINATION_
  26. #include "UserTermination.h"
  27. #endif
  28.  
  29.  
  30. // _________________________________________________________________________________________________________ //
  31. // TGDevice class member function implementations
  32.  
  33. //    CONSTRUCTORS & DESTRUCTORS
  34. #pragma segment UserTermination
  35. TUserTermination::TUserTermination(char ASCIIValue)
  36. // The default constructor wants an ASCII char, such as '.' as value (that is default).
  37. // It will call the initialize function that will map the ASCII value to a virtual key code.
  38. {
  39.     long auxVersion = 0;
  40.  
  41.     // Initialize fields to known values
  42.     fChar = ASCIIValue;
  43.     fKeyCode = 0;
  44.  
  45.     // Test if A/UX is present or not, needed later for the event handling code
  46.     OSErr anErr = Gestalt(gestaltAUXVersion, &auxVersion);
  47.     if (anErr == gestaltUnknownErr || auxVersion == 0)
  48.         fAUX = false;
  49.     else
  50.         fAUX = true;
  51.  
  52.     // Do any other dynamic initialization
  53.     this->Initialize();
  54. }
  55.  
  56.  
  57. #pragma segment UserTermination
  58. TUserTermination::~TUserTermination()
  59. // Default constructor, nothing just now.
  60. {
  61. }
  62.  
  63.  
  64. //    INITIALIZATION ROUTINES    
  65. #pragma segment UserTermination
  66. void TUserTermination::Initialize()
  67. // Initialize will fetch the KCHR handle, and convert the ASCII character to
  68. // a real virtual key code.
  69. {
  70.     Handle hKCHR = NULL;
  71.     Ptr pKCHR;
  72.     short KCHRId;
  73.     Boolean notDone;
  74.     unsigned long state;
  75.     long ktResult;
  76.  
  77.     pKCHR = (Ptr)GetScriptManagerVariable(smKCHRCache);        // try to get the pointer directly
  78.     if (!pKCHR)                                    // maybe pre-System7
  79.     {
  80.         KCHRId = (short)GetScriptVariable(short(GetScriptManagerVariable(smKeyScript)), smScriptKeys);
  81.         hKCHR = GetResource('KCHR', KCHRId);    // get the KCHR resource
  82.         pKCHR = *hKCHR;                            // get the pointer
  83.     }
  84.     notDone = true;
  85.  
  86.     if (pKCHR)
  87.     {
  88.         state = 0;
  89.  
  90.         while ((fKeyCode <= 0x7F) && notDone)
  91.             // loop through all possible keycodes
  92.             {
  93.                 ktResult = KeyTrans((pKCHR), fKeyCode, &state);// get the ASCII value
  94.                 if (((ktResult & ktAscii1Mask) == fChar) || ((ktResult & ktAscii2Mask) == fChar))// check result for desited real char
  95.                     notDone = false;
  96.                 else
  97.                     fKeyCode++;                    // keep looking
  98.             }
  99.     }
  100.     // Clean up
  101.     if (hKCHR)
  102.         ReleaseResource(hKCHR);
  103.  
  104.     if (notDone)
  105.         fKeyCode = -1;                            // set an insane value
  106.  
  107.     return;                                        // job done    
  108. }
  109.  
  110.  
  111. //    MAIN INTERFACE
  112.  
  113. #pragma segment UserTermination
  114. Boolean TUserTermination::Abort()
  115. //    Abort returns true if the user has pressed Command-Period.
  116. //    It does this by walking the event queue.  Walking the event queue is not
  117. //    recommended, but since we want a Command-Period event to take priority over
  118. //    any other events in the queue, looking ahead in the queue is the only way.
  119. //
  120. //    Note that for A/UX, this technique does not work as A/UX does not support
  121. //    the Macintosh event queue structure.  In that case, we will call CheckAUXEventQueue
  122. //    to find the result.
  123. {
  124.     Boolean foundEvent = false;
  125.     EvQElPtr eventQPtr;
  126.     QHdrPtr eventQHdr;
  127.  
  128.  
  129.     eventQHdr = GetEvQHdr();                // get the event queue
  130.     eventQPtr = (EvQElPtr)eventQHdr->qHead;    // get the first element
  131.  
  132.     while (eventQPtr &&!foundEvent)
  133.     {
  134.         foundEvent = (((eventQPtr->evtQMessage & keyCodeMask) >> 8) == fKeyCode && (eventQPtr->evtQModifiers & cmdKey));// the event we want + cmd key?
  135.  
  136.         if (!foundEvent)
  137.             eventQPtr = (EvQElPtr)eventQPtr->qLink;// get the next element in the queue
  138.     }
  139.     return foundEvent;
  140. }
  141.  
  142.  
  143. //    PRIVATE INTERNAL FUNCTIONS
  144.  
  145. /*#pragma segment UserTermination
  146. Boolean TUserTermination::CheckAUXEventQueue(char keyCode,
  147.                                              short modifiers)
  148. {
  149.     struct
  150.     {
  151.         EventRecord mask;
  152.         EventRecord value;
  153.     } eventFilter;
  154.  
  155.     eventFilter.mask.what = everyEvent;
  156.     eventFilter.value.what = keyDown;            // look for a keydown event
  157.     eventFilter.mask.message = keyCodeMask;
  158.     eventFilter.value.message = keyCode << 8;    // Set keyCode
  159.     eventFilter.mask.modifiers = modifiers;
  160.     eventFilter.value.modifiers = modifiers;    // Set modifiers
  161.     eventFilter.mask.when = 0;
  162.     eventFilter.mask.where.v = 0;                // Zero out other stuff
  163.     eventFilter.mask.where.h = 0;
  164.     return ((AUXDispatch(kAUXFindEvent, (char*) & eventFilter) > 0) && (eventFilter.value.what != nullEvent));// Have A/UX look for us
  165. }*/
  166.  
  167.  
  168. // _________________________________________________________________________________________________________ //
  169.  
  170. /*    Change History (most recent last):
  171.   No        Init.    Date        Comment
  172.   1            khs        9/26/92        New file
  173. */
  174.